package com.ciwong.epaper.common.animdialogactivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.ShareBean;
import com.ciwong.epaper.modules.me.bean.MedalsBean;
import com.ciwong.epaper.modules.share.QQShareHelper;
import com.ciwong.epaper.modules.share.WxShareHelper;
import com.ciwong.epaper.modules.share.bean.ShareMsgObj;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.FileUtil;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.Str2BitmapUtil;
import com.ciwong.epaper.util.ViewUtil;
import com.ciwong.epaper.util.Xml2BitmapUtils;
import com.ciwong.epaper.widget.NewLoginDialog;
import com.ciwong.epaper.widget.ShareDialogResult;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.connect.common.Constants;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class AnimIntegralActivityDialog extends BaseActivity {
    private ImageView iv_close_page, iv_share_bt, iv_dialog_medal, iv_qr_code;
    private SimpleDraweeView iv_dialog_header;
    private TextView tv_user_name, tv_des_congratulate, tv_medal_name, tv_rarity, tv_des_content;


    private View ll_share_view;
    private RatingBar ratingBar_rarity;
    public LottieAnimationView mLottieAnimationView;


    private MedalsBean medalsBean;
    private LinkedList<MedalsBean> medalsBeanLinkedList;
    private UserInfoBase userInfoBase;

    @Override
    protected void preCreate() {
        super.preCreate();
        List<MedalsBean> listMedal = (List<MedalsBean>) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
        medalsBeanLinkedList = new LinkedList<>();
        medalsBeanLinkedList.addAll(listMedal);
    }

    @Override
    protected int setView() {
        return R.layout.dialog_integral_view;
    }

    @Override
    protected void findViews() {
        hideTitleBar();//隐藏标题
        ll_share_view = findViewById(R.id.ll_share_view);
        iv_close_page = findViewById(R.id.iv_close_page);
        iv_share_bt = findViewById(R.id.iv_share_bt);

        iv_dialog_header = (SimpleDraweeView) findViewById(R.id.iv_dialog_header);
        iv_dialog_medal = findViewById(R.id.iv_dialog_medal);
        iv_qr_code = findViewById(R.id.iv_qr_code);
        ratingBar_rarity = findViewById(R.id.ratingBar_rarity);
        tv_user_name = findViewById(R.id.tv_user_name);
        tv_des_congratulate = findViewById(R.id.tv_des_congratulate);
        tv_medal_name = findViewById(R.id.tv_medal_name);
        tv_rarity = findViewById(R.id.tv_rarity);
        tv_des_content = findViewById(R.id.tv_des_content);
        mLottieAnimationView = findViewById(R.id.animation_view);
        iv_qr_code.setVisibility(View.INVISIBLE);
        initOnclick();
    }

    @Override
    protected void initEvent() {
        overridePendingTransition(R.anim.activity_interagl_in, R.anim.activity_interagl_out);
    }

    private void startActivity(LinkedList<MedalsBean> medalsBeanLinkedList) {
        if (medalsBeanLinkedList != null && medalsBeanLinkedList.size() > 0) {
            Intent intent = new Intent(this, AnimIntegralActivityDialog.class);
            intent.putExtra(IntentFlag.INTENT_FLAG_OBJ_LIST, (Serializable) medalsBeanLinkedList);
            startActivity(intent);
            finish();
        }

    }

    @Override
    protected void init() {
        userInfoBase = EApplication.getInstance().getUserInfoBase();
        setValideSource(false);
    }

    @Override
    protected void loadData() {
        if (medalsBeanLinkedList != null && medalsBeanLinkedList.size() > 0) {
            MedalsBean medalsBean = medalsBeanLinkedList.removeFirst();
            refrashData(medalsBean);
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.activity_interagl_out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        WxShareHelper.getInstance().onResumeResultData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_QQ_SHARE || requestCode == Constants.REQUEST_QZONE_SHARE) {
            QQShareHelper.getInstance().onActivityResultData(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initOnclick() {
        iv_close_page.setOnClickListener(new View.OnClickListener() {//关闭页面
            @Override
            public void onClick(View v) {
                if (medalsBeanLinkedList != null && medalsBeanLinkedList.size() > 0) {
                    startActivity(medalsBeanLinkedList);
                } else {
                    finish();
                }
            }
        });
        iv_share_bt.setOnClickListener(new View.OnClickListener() {//分享
            @Override
            public void onClick(View v) {
                if (EConstants.IS_YOUKE) {
                    NewLoginDialog newLoginDialog = new NewLoginDialog(AnimIntegralActivityDialog.this);
                    newLoginDialog.showLoginDialog();
                } else {
                    makeShareBitmap(medalsBean);
                }
            }
        });
    }

    private boolean flagMakeBitmapSuccess = false;

    private void makeShareBitmap(final MedalsBean medalsBean) {
        flagMakeBitmapSuccess = false;
        showCricleProgress();
        ViewInflater.getViewWithData(AnimIntegralActivityDialog.this, medalsBean, userInfoBase, new ViewInflater.CallBack() {
            @Override
            public void callback(boolean makeBitmapSuccess, View view) {
                hideCricleProgress();
                flagMakeBitmapSuccess = makeBitmapSuccess;
                if (flagMakeBitmapSuccess) {//图片加载成功再去剪切
                    Xml2BitmapUtils.saveCroppedImage(AnimIntegralActivityDialog.this, view, FileUtil.makeShareIntegralDialogLocalPath(AnimIntegralActivityDialog.this));
                    setShareButton();
                }
            }
        });

    }

    /**
     * 设置分享按钮（展示与隐藏）
     *
     */
    private void setShareButton() {
        ShareBean shareBean = getShareBean(AnimIntegralActivityDialog.this);
        ShareDialogResult shareDialog = new ShareDialogResult(AnimIntegralActivityDialog.this);
        shareDialog.setShareBean(shareBean);
        shareDialog.show();
    }

    private static ShareBean getShareBean(Context context) {
        ShareBean shareBean = new ShareBean();
        shareBean.setShareType(ShareMsgObj.ShareType.SHARE_IMAGE);
        shareBean.setFileLocalPath(FileUtil.getShareIntegralDialogLocalPath(context.getApplicationContext()));
        shareBean.setTitle(context.getString(R.string.app_name));
        return shareBean;
    }

    public void refrashData(MedalsBean medalsBean) {
        this.medalsBean = medalsBean;

        showQRCodeBitmap();
        refrashImageViewdate(medalsBean, userInfoBase);
        refrashtextviewdate(medalsBean, userInfoBase);
        mLottieAnimationView.playAnimation();
    }

    private void refrashImageViewdate(MedalsBean medalsBean, UserInfoBase userInfoBase) {
        if (userInfoBase != null) {
            if (!TextUtils.isEmpty(userInfoBase.getAvatar())) {
                Uri uri = Uri.parse(userInfoBase.getAvatar());
                iv_dialog_header.setImageURI(uri);
            } else {
                ViewUtil.setLocalDefaultImg(iv_dialog_header, R.mipmap.head);
            }
        }
        ImageLoader.getInstance().displayImage(medalsBean.getIcon() + "?x-oss-process=style/square_420", iv_dialog_medal, EConstants.getDefaultpictureOption(R.mipmap.icon_medal_big));
    }

    private void refrashtextviewdate(MedalsBean medalsBean, UserInfoBase userInfoBase) {

        if (userInfoBase != null)
            tv_user_name.setText(userInfoBase.getRealName());
        tv_medal_name.setText(medalsBean.getMedalName());
        if (medalsBean != null)
            ratingBar_rarity.setRating(medalsBean.getRarity());
        tv_des_content.setText(medalsBean.getRemark());
    }

    protected int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getApplication().getResources().getDisplayMetrics());
    }

    public Bitmap showQRCodeBitmap() {
        Bitmap qrCodeWithLogo = Str2BitmapUtil.createQRCodeWithLogo(getApplication().getResources().getString(R.string.download_url),
                800, BitmapFactory.decodeResource(getApplication().getResources(), R.mipmap.ic_launcher), dp2px(52), dp2px(52));
        iv_qr_code.setImageBitmap(qrCodeWithLogo);
        return qrCodeWithLogo;
    }


}
